package com.lzp.batch;

import com.lzp.entity.WechatBill;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DefaultFieldSetFactory;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : lzp
 * @date : 2021/9/23 19:15
 * @describe : TODO
 * @vision 1.0
 */
@Component
public class MyExcelReader<T> extends FlatFileItemReader<T> {
    static {
        System.out.println("父类静态代码块执行");
    }
     {
         System.out.println("父类类代码块执行");

        DefaultLineMapper<T> objectDefaultLineMapper = new DefaultLineMapper<>();
        objectDefaultLineMapper.setLineTokenizer(new DelimitedLineTokenizer());
        objectDefaultLineMapper.setFieldSetMapper(new BeanWrapperFieldSetMapper());
        setLineMapper(objectDefaultLineMapper);
    }
    private static void test(){
        System.out.println("父类静态方法");
    }
//    public void executeReaderClass(Class<T> clz, Resource resource) {
////        ClassPathResource resource = new ClassPathResource("bill"+ File.separator+"微信支付账单(20160826-20161126).csv");
////        setResource(resource);
//////        DefaultLineMapper<WechatBill> defaultLineMapper = new DefaultLineMapper<WechatBill>();
////        DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer();
////        delimitedLineTokenizer.setFieldSetFactory(new DefaultFieldSetFactory());
////        Field[] fields = clz.getDeclaredFields();
////        List<String> list = new ArrayList<>();
////        for (Field field : fields) {
////            if (!Modifier.isStatic(field.getModifiers())) {
////                list.add(field.getName());
////            }
////        }
////        String[] names = new String[list.size()];
////        delimitedLineTokenizer.setNames(list.toArray(names));
////        delimitedLineTokenizer.setDelimiter(",");
////        defaultLineMapper.setLineTokenizer(delimitedLineTokenizer);
////        BeanWrapperFieldSetMapper<WechatBill> fieldSetMapper = new BeanWrapperFieldSetMapper<WechatBill>();
////        fieldSetMapper.setTargetType(clz);
////        defaultLineMapper.setFieldSetMapper(fieldSetMapper);
////        setLineMapper(defaultLineMapper);
//    }

}
